/*******************************************************************************
Chatty - Mobile Messaging - J2ME ICQ clone
Copyright (C) 2003-05  Chatty Project

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
********************************************************************************
File: src/chatty/DebugLog.java
Version: ###VERSION###  Date: ###DATE###
Author(s): Artyomov Denis
*******************************************************************************/

// #sijapp cond.if modules_MAGIC_EYE is "true" #
package chatty.modules;

import DrawControls.*;
import DrawControls.text.Parser;

import java.util.Vector;
import javax.microedition.lcdui.*;

import chatty.*;
import chatty.cl.*;
import chatty.comm.*;
import chatty.ui.*;
import chatty.ui.menu.*;
import chatty.util.*;

import protocol.*;

public class MagicEye extends TextListEx implements SelectListener {
    private static MagicEye instance = new MagicEye();
    private Vector uins = new Vector();
    private Vector protocols = new Vector();
    
    private MagicEye() {
        super(ResBundle.getString("magic eye"));

        MenuModel menu = new MenuModel(MenuModel.MENU_LEFT);
        menu.addItem("user_menu",     Chatty.chatIcons.iconAt(Chatty.ICON_USER_MENU),	MENU_USER_MENU);
        menu.addItem("copy_text",     Chatty.chatIcons.iconAt(Chatty.ICON_COPY), 		MENU_COPY);
		if (!ChattyUI.isClipBoardEmpty()) { 
		    menu.addItem("copy_add",      Chatty.chatIcons.iconAt(Chatty.ICON_COPY_ADD),MENU_COPY_ADD);
		}
        menu.addItem("copy_all_text", Chatty.chatIcons.iconAt(Chatty.ICON_COPY_ALL), 	MENU_COPY_ALL);
		
        menu.addItem("clear",         Chatty.chatIcons.iconAt(Chatty.ICON_CLEAR),		MENU_CLEAN);
        menu.addItem("back",          Chatty.icons.iconAt(Chatty.ICON_BACK),		    MENU_BACK);
        menu.setActionListener(this);
        menu.setDefaultItemCode(MENU_COPY);
        setMenu(menu, MENU_BACK, MENU_USER_MENU);
    }
    
    public static void activate() {
        instance.setCurrentItem(instance.getSize());
        instance.show();
    }

    private synchronized void registerAction(Protocol protocol, String uin, String action, String msg) {
        uins.addElement(uin);
        protocols.addElement(protocol);

        int counter = uiBigTextIndex++;
        String date = Util.getDateString(false);
        action = ResBundle.getString(action);
        Contact contact = protocol.getItemByUIN(uin);

        Parser record = createParser(counter);
        record.useMinHeight();
        record.addText(date + ": ", THEME_MAGIC_EYE_NUMBER, FONT_STYLE_PLAIN);
        if (null == contact) {
            record.addText(uin, THEME_MAGIC_EYE_NL_USER, FONT_STYLE_PLAIN);
        } else {
            record.addText(contact.getName(),
                    THEME_MAGIC_EYE_USER, FONT_STYLE_PLAIN);
        }
        record.doCRLF();
        record.addText(action, THEME_MAGIC_EYE_ACTION, FONT_STYLE_PLAIN);
        if (null != msg) {
            record.doCRLF();
            record.addText(msg, THEME_MAGIC_EYE_TEXT, FONT_STYLE_PLAIN);
        }

        lock();
        addPar(record);
        removeOldRecords();
        unlock();
    }
    
    private void removeOldRecords() {
        final int maxRecordCount = 50;
        while (maxRecordCount < uins.size()) {
            protocols.removeElementAt(0);
            uins.removeElementAt(0);
            removeFirstText();
        }
    }

//    public static final int ACTTYPE_SYSNOTICE   = 0;
//    public static final int ACT_ANTISPAM        = 1;
//    public static final int ACTTYPE_READ_STATUS = 2;
//    public static final int ACT_INVISIBLE_CHECK = 4;
//
    public static void addAction(Protocol protocol, String uin, String action, String msg) {
        instance.registerAction(protocol, uin, action, msg);
    }

    public static void addAction(Protocol protocol, String uin, String action) {
        instance.registerAction(protocol, uin, action, null);
    }

    private static final int MENU_COPY      = 0;
	private static final int MENU_COPY_ADD  = 1;
    private static final int MENU_COPY_ALL  = 2;
    private static final int MENU_CLEAN     = 3;
    private static final int MENU_USER_MENU = 4;
    private static final int MENU_BACK      = 5;
    
    public void select(Select select, MenuModel model, int cmd) {
        switch (cmd) {
            case MENU_COPY:
            case MENU_COPY_ALL:
                copy(MENU_COPY_ALL == cmd);
                break;

			case MENU_COPY_ADD:
                copy_add();
                break;
				
            case MENU_CLEAN:
                synchronized (instance) {
                    uins.removeAllElements();
                    protocols.removeAllElements();
                    clear();
                }
                break;

            case MENU_USER_MENU:
                try {
                    int item = getCurrTextIndex();
                    String uin = (String)uins.elementAt(item);
                    Protocol protocol = (Protocol)protocols.elementAt(item);
                    MenuModel m = protocol.createTempContact(uin).getContextMenu();
                    if (null != m) {
                        new Select(m).show();
                    }
                } catch (Exception e) {
                }
                return;

            case MENU_BACK:
                back();
                return;
        }
        select.back();
    }
}
// #sijapp cond.end#